package com.hnust.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hnust.entity.code.BuildCode;
import com.hnust.entity.code.FloorCode;
import com.hnust.entity.code.RegionCode;
import com.hnust.entity.code.RoomCode;
import com.hnust.entity.dto.CodeDto;
import com.hnust.mapper.RegionCodeMapper;
import com.hnust.service.BuildCodeService;
import com.hnust.service.FloorCodeService;
import com.hnust.service.RegionCodeService;
import com.hnust.service.RoomCodeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 创建人：zhangRuWang
 * 创建时间：2022/5/5
 * 描述：
 */
@Service
public class RegionCodeServiceImpl extends ServiceImpl<RegionCodeMapper, RegionCode> implements RegionCodeService {
    @Autowired
    private BuildCodeService buildCodeService;
    @Autowired
    private FloorCodeService floorCodeService;
    @Autowired
    private RoomCodeService roomCodeService;
    @Override
    public List<CodeDto> codeSearch(String codeName,Integer type) {
        List<CodeDto> dtos=null;
        if(type==1){
            QueryWrapper<RegionCode> queryWrapper1=new QueryWrapper<>();
            queryWrapper1.like(codeName!=null,"region_name",codeName);
            List<RegionCode> regionCodes=this.list(queryWrapper1);
            dtos=regionCodes.stream().map((item)->{
                CodeDto c=new CodeDto();
                BeanUtils.copyProperties(item,c);
                c.setName(item.getRegionName());
                return c;
            }).collect(Collectors.toList());
        }else if(type==2){
            QueryWrapper<BuildCode> queryWrapper2=new QueryWrapper<>();
            queryWrapper2.like(codeName!=null,"build_name",codeName);
            List<BuildCode> buildCodes=buildCodeService.list(queryWrapper2);
            dtos=buildCodes.stream().map((item)->{
                CodeDto c=new CodeDto();
                BeanUtils.copyProperties(item,c);
                c.setName(item.getBuildName());
                return c;
            }).collect(Collectors.toList());
        }else if(type==3){
            QueryWrapper<FloorCode> queryWrapper3=new QueryWrapper<>();
            queryWrapper3.like(codeName!=null,"floor_name",codeName);
            List<FloorCode> floorCodes=floorCodeService.list(queryWrapper3);
            dtos=floorCodes.stream().map((item)->{
                CodeDto c=new CodeDto();
                BeanUtils.copyProperties(item,c);
                c.setName(item.getFloorName());
                return c;
            }).collect(Collectors.toList());
        }else if(type==4){
            QueryWrapper<RoomCode> queryWrapper4=new QueryWrapper<>();
            queryWrapper4.like(codeName!=null,"room_name",codeName);
            List<RoomCode> roomCodes=roomCodeService.list(queryWrapper4);
            dtos=roomCodes.stream().map((item)->{
                CodeDto c=new CodeDto();
                BeanUtils.copyProperties(item,c);
                c.setName(item.getRoomName());
                return c;
            }).collect(Collectors.toList());
        }
        return dtos;
    }

    @Override
    public void codeDelete(String id, Integer type) {
        if(type==1){
            this.removeById(id);
        }else if(type==2){
            buildCodeService.removeById(id);
        }else if(type==3){
            floorCodeService.removeById(id);
        }else{
            roomCodeService.removeById(id);
        }
    }

    @Override
    public void codeModify(CodeDto codeDto) {
        if(codeDto.getType()==1){
            RegionCode r=new RegionCode();
            r.setId(codeDto.getId());
            r.setCode(codeDto.getCode());
            r.setRegionName(codeDto.getName());
            this.updateById(r);
        }else if(codeDto.getType()==2){
            BuildCode b=new BuildCode();
            b.setId(codeDto.getId());
            b.setCode(codeDto.getCode());
            b.setBuildName(codeDto.getName());
            buildCodeService.updateById(b);
        }else if(codeDto.getType()==3){
            FloorCode f=new FloorCode();
            f.setId(codeDto.getId());
            f.setCode(codeDto.getCode());
            f.setFloorName(codeDto.getName());
            floorCodeService.updateById(f);
        }else if(codeDto.getType()==4){
            RoomCode r=new RoomCode();
            r.setId(codeDto.getId());
            r.setCode(codeDto.getCode());
            r.setRoomName(codeDto.getName());
            roomCodeService.updateById(r);
        }
    }

    @Override
    public void codeAdd(CodeDto codeDto) {
        if(codeDto. getType()==1){
            RegionCode r=new RegionCode();
            r.setCode(codeDto.getCode());
            r.setRegionName(codeDto.getName());
            this.save(r);
        }else if(codeDto.getType()==2){
            BuildCode b=new BuildCode();
            b.setCode(codeDto.getCode());
            b.setBuildName(codeDto.getName());
            buildCodeService.save(b);
        }else if(codeDto.getType()==3){
            FloorCode f=new FloorCode();
            f.setCode(codeDto.getCode());
            f.setFloorName(codeDto.getName());
            floorCodeService.save(f);
        }else if(codeDto.getType()==4){
            RoomCode r=new RoomCode();
            r.setCode(codeDto.getCode());
            r.setRoomName(codeDto.getName());
            roomCodeService.save(r);
        }
    }

    @Override
    public List<CodeDto> codeList(Integer type) {
        List<CodeDto> dtos=null;
        if(type==1){
            dtos=this.list().stream().map((item)->{
                CodeDto c=new CodeDto();
                c.setCode(item.getCode());
                c.setName(item.getRegionName());
                return c;
            }).collect(Collectors.toList());
        }else if(type==2){
            dtos=buildCodeService.list().stream().map((item)->{
                CodeDto c=new CodeDto();
                c.setCode(item.getCode());
                c.setName(item.getBuildName());
                return c;
            }).collect(Collectors.toList());
        }else if(type==3){
            dtos=floorCodeService.list().stream().map((item)->{
                CodeDto c=new CodeDto();
                c.setCode(item.getCode());
                c.setName(item.getFloorName());
                return c;
            }).collect(Collectors.toList());
        }else if(type==4){
            dtos=roomCodeService.list().stream().map((item)->{
                CodeDto c=new CodeDto();
                c.setCode(item.getCode());
                c.setName(item.getRoomName());
                return c;
            }).collect(Collectors.toList());
        }
        return dtos;
    }
}
